package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjWorkModule;
import com.zb.entity.ZjWorkSla;
import com.zb.entity.ZjWorkSlaModule;
import com.zb.extend.WorkSlaModuleExtend;
import com.zb.mapper.ZjWorkSlaModuleMapper;
import com.zb.service.ZjWorkModuleService;
import com.zb.service.ZjWorkSlaModuleService;
import com.zb.service.ZjWorkSlaService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 工单SLA模块表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-02-04
 */
@Service
public class ZjWorkSlaModuleServiceImpl extends ServiceImpl<ZjWorkSlaModuleMapper, ZjWorkSlaModule> implements ZjWorkSlaModuleService {
    @Resource
    private ZjWorkModuleService workModuleService;
    @Resource
    private ZjWorkSlaService workSlaService;

    @Override
    public void save(Long custAccId, Long workSlaId, List<Long> moduleIds) {
        // 根据工单SlaId获取已添加的模块ID
        List<Long> moduleIdList = getModuleIdByCustaccId(custAccId, workSlaId);
        if (CollectionUtil.isNotEmpty(moduleIdList)) {
            List<Long> existModuleIds = moduleIds.stream().filter(f -> moduleIdList.stream().anyMatch(f2 -> Objects.equals(f2, f))).distinct().toList();
            if (CollectionUtil.isNotEmpty(existModuleIds)) {
                List<ZjWorkModule> moduleList = workModuleService.getPartInfoByIds(existModuleIds);
                Assert.isTrue(false, "模块已在工单SLA中被引用，禁止重复引用模块：" + (moduleList.stream().map(ZjWorkModule::getName).collect(Collectors.joining("、"))));
            }
        }
        List<ZjWorkSlaModule> slaModules = new ArrayList<>();
        ZjWorkSlaModule module = null;
        for (Long moduleId : moduleIds) {
            module = new ZjWorkSlaModule();
            module.setId(IdWorker.getId());
            module.setCustAccId(custAccId);
            module.setWorkSlaId(workSlaId);
            module.setModuleId(moduleId);
            slaModules.add(module);
        }
        deleteByWorkSlaId(workSlaId);
        this.saveBatch(slaModules);
    }

    // 保存模块与Sla关联
    @Override
    public void save(Long custAccId, Long moduleId, Long workSlaId) {
        if (null == workSlaId || workSlaId == 0) {
            return;
        }
        QueryWrapper<ZjWorkSlaModule> wrapper = new QueryWrapper<>();
        wrapper.select("id")
                .eq("module_id", moduleId);
        List<ZjWorkSlaModule> moduleList = this.list(wrapper);
        if (CollectionUtil.isEmpty(moduleList)) {
            ZjWorkSlaModule slaModule = new ZjWorkSlaModule();
            slaModule.setId(IdWorker.getId());
            slaModule.setCustAccId(custAccId);
            slaModule.setModuleId(moduleId);
            slaModule.setWorkSlaId(workSlaId);
            this.save(slaModule);
            return;
        }
        for (ZjWorkSlaModule module : moduleList) {
            if (Objects.equals(module.getWorkSlaId(), workSlaId)){
                return;
            }
            module.setWorkSlaId(workSlaId);
            this.updateById(module);
            return;
        }
    }

    // 根据工单SLA表ID删除模块
    @Override
    public void deleteByWorkSlaId(Long workSlaId) {
        List<Long> workSlaIds = new ArrayList<>();
        workSlaIds.add(workSlaId);
        deleteByWorkSlaIds(workSlaIds);
    }

    // 根据工单SLA表ID删除模块
    @Override
    public void deleteByWorkSlaIds(List<Long> workSlaIds) {
        if (CollectionUtil.isEmpty(workSlaIds)) {
            return;
        }
        QueryWrapper<ZjWorkSlaModule> wrapper = new QueryWrapper<>();
        wrapper.in("work_sla_id", workSlaIds);
        this.remove(wrapper);
    }

    // 根据商家ID获取已选择的适用模块
    @Override
    public List<Long> getModuleIdByCustaccId(Long custAccId, Long workSlaId) {
        QueryWrapper<ZjWorkSlaModule> wrapper = new QueryWrapper<>();
        wrapper.select("module_id")
                .eq("cust_acc_id", custAccId);
        if (null != workSlaId) {
            wrapper.ne("work_sla_id", workSlaId);
        }
        return this.list(wrapper).stream().map(ZjWorkSlaModule::getModuleId).distinct().toList();
    }

    // 根据工单策略SLA ID获取模块
    @Override
    public List<WorkSlaModuleExtend> getByWorkSlaIds(List<Long> workSlaIds) {
        if (CollectionUtil.isEmpty(workSlaIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkSlaModule> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_sla_id", "module_id")
                .in("work_sla_id", workSlaIds);
        List<ZjWorkSlaModule> slaModuleList = this.list(wrapper);
        List<ZjWorkModule> moduleList = workModuleService.getPartInfoByIds(slaModuleList.stream().map(ZjWorkSlaModule::getModuleId).distinct().toList());
        return slaModuleList.stream().map(sla -> {
            WorkSlaModuleExtend extend = new WorkSlaModuleExtend();
            BeanUtil.copyProperties(sla, extend);
            moduleList.stream().filter(f -> Objects.equals(f.getId(), sla.getModuleId())).map(ZjWorkModule::getName).findFirst().ifPresent(extend::setModule);
            return extend;
        }).toList();
    }

    @Override
    public List<Long> getModuleIdByWorkSlaId(Long workSlaId) {
        QueryWrapper<ZjWorkSlaModule> wrapper = new QueryWrapper<>();
        wrapper.select("module_id")
                .eq("work_sla_id", workSlaId);
        return this.list(wrapper).stream().map(ZjWorkSlaModule::getModuleId).distinct().toList();
    }

    // 根据模块ID获取SLA规则名称
    @Override
    public ZjWorkSla getSalNameByModuleId(Long moduleId) {
        QueryWrapper<ZjWorkSlaModule> wrapper = new QueryWrapper<>();
        wrapper.select("work_sla_id")
                .eq("module_id", moduleId);
        ZjWorkSlaModule slaModule = this.getOne(wrapper);
        if (null == slaModule) {
            return null;
        }
        ZjWorkSla workSla = workSlaService.getById(slaModule.getWorkSlaId());
        return workSla;
    }

    // 根据模块ID获取SLA的关联模块
    @Override
    public List<ZjWorkSlaModule> getByModuleIds(List<Long> moduleIds) {
        if (CollectionUtil.isEmpty(moduleIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkSlaModule> wrapper = new QueryWrapper<>();
        wrapper.select("work_sla_id", "module_id")
                .in("module_id", moduleIds);
        return this.list(wrapper);
    }
}
